package com.jd.risk.bankorder.service.privilege;

import com.jd.risk.bankorder.common.CommonResult;
import com.jd.risk.bankorder.dao.privilege.ResourceMapper;
import com.jd.risk.bankorder.entity.privilege.Resource;
import com.jd.risk.bankorder.entity.privilege.ResourceExample;
import com.jd.risk.bankorder.entity.privilege.Role;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;



@Service
public class ResourceService {
	
	
	@Autowired
	private ResourceMapper resourceMapper;
	
	@Autowired
	private RoleService roleService;
	private List<Resource> allMenus;

	public List<Resource> getReourceNodes() {
		
		ResourceExample resourceExample = new ResourceExample();
		resourceExample.setOrderByClause("parent_ids ASC");
		List<Resource> res = resourceMapper.selectByExample(resourceExample);
		return res;
	}



	public CommonResult<Resource> saveOrUpdateResource(Resource resource) {
		
		CommonResult<Resource> result = new CommonResult<Resource>();
		
		if(null == resource.getId())
		{
			resource.setCreatedDate(new Date());
			resource.setModifiedDate(new Date());
			resourceMapper.insertSelective(resource);
		}else{
			resourceMapper.updateByPrimaryKeySelective(resource);
		}
		
		Resource rnode  = resourceMapper.selectByPrimaryKey(resource.getId());
		List<Resource> rnodes = new ArrayList<Resource>();
		rnodes.add(rnode);
		result.setRows(rnodes);
		result.setSuccess(true);
		result.setMessage("成功");
		return result;
	}



	public CommonResult<String> deleteResource(Resource resource) {
		
		CommonResult<String> result = new CommonResult<String>();
		int ret = resourceMapper.deleteByPrimaryKey(resource.getId());
		if(ret>0)
		{
			result.setSuccess(true);
		}else{
			result.setSuccess(false);
			result.setMessage("数据库插入失败");
		}
		return result;
	}



	public Resource getResourceById(
			long parseLong) {
		
		return resourceMapper.selectByPrimaryKey(parseLong);
	}



	public Set<String> getResourceStrByRoleIds(String roleIds) {
		Set<String> resourcestrSet = new HashSet<String>();
		Set<Role> roles = roleService.getRoleByRoleIds(roleIds);
		Set<Resource> resourceSet = new HashSet<Resource>();
		for(Role role :roles)
		{
			String resIds = role.getResourceIds();
			resourceSet.addAll(getResourceByResourceIds(resIds));
		}
		for(Resource resource :resourceSet)
		{
			if(StringUtils.isNotBlank(resource.getPermission()))
			{
				resourcestrSet.add(resource.getPermission());
			}
		}
		return resourcestrSet;
	}

	public Set<Resource> getResourceByResourceIds(String resIds) {
		Set<Resource> resourceSet = new HashSet<Resource>();
		if (StringUtils.isNotBlank(resIds) && resIds.contains(",")) {
			String[] idseg = resIds.split(",");
			for (String id : idseg) {
				id = id.trim();
				Resource resource = getResourceById(Long.parseLong(id));
				resourceSet.add(resource);
			}
		}

		return resourceSet;
	}

    public List<Resource> getAllMenus() {

		ResourceExample resourceQuery = new ResourceExample();
		resourceQuery.createCriteria().andTypeEqualTo("1");
		return resourceMapper.selectByExample(resourceQuery);


    }
}
